#ifndef __LSM303_H
#define __LSM303_H


#include "main.h"
#include "myiic.h"
#include "math.h"


#define LSM303_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE()

#define PI	3.1415926

#define LSM303_ACCELE_ADDRESS		0x19
#define LSM303_MAGN_ADDRESS			0x1E


#define CTRL_REG1_A			0x20
#define CTRL_REG2_A			0x21
#define CTRL_REG3_A			0x22
#define CTRL_REG4_A			0x23
#define CTRL_REG5_A			0x24
#define CTRL_REG6_A			0x25

#define STATUS_REG_A		0x27

#define OUT_X_H_A 			0x28
#define OUT_X_L_A   		0x29
#define OUT_Z_H_A  			0x2A
#define OUT_Z_L_A  			0x2B
#define OUT_Y_H_A  			0x2C
#define OUT_Y_L_A				0x2D

#define CRA_REG_M				0x00
#define CRB_REG_M				0x01
#define MR_REG_M				0x02

#define OUT_X_H_M 			0x03
#define OUT_X_L_M   		0x04
#define OUT_Z_H_M  			0x05
#define OUT_Z_L_M  			0x06
#define OUT_Y_H_M  			0x07
#define OUT_Y_L_M				0x08



extern iic_bus_struct lsm303_iic_struct;


void LSM303_ACCELE_Write(u8 reg, u8 data);
void LSM303_ACCELE_Write_Bytes(u8 reg, u8* buff, u8 len);
void LSM303_MAGN_Write(u8 reg, u8 data);
void LSM303_MAGN_Write_Bytes(u8 reg, u8* buff, u8 len);

u8 LSM303_ACCELE_Read(u8 reg);
void LSM303_ACCELE_Read_Bytes(u8 reg, u8* buff, u8 len);
u8 LSM303_MAGN_Read(u8 reg);
void LSM303_MAGN_Read_Bytes(u8 reg, u8* buff, u8 len);


u8 LSM303_ACCELE_Init(void);
void LSM303_MAGN_Init(void);
u8 LSM303_Init(void);

void LSM303_GET_ADATA(int16_t* AX, int16_t* AY, int16_t* AZ);
void LSM303_GET_MDATA(int16_t* MX, int16_t* MY, int16_t* MZ);
int LSM303_CALC_AZ(int16_t AX, int16_t AY, int16_t AZ);
int LSM303_CALC_AX(int16_t AX, int16_t AY, int16_t AZ);

int LSM303_CALC_M(int16_t MX, int16_t MY, int16_t MZ);

float Azimuth_CALC(int16_t AX, int16_t AY, int16_t AZ, int16_t MX, int16_t MY, int16_t MZ);


void LSM303DLH_Sleep(void);
void LSM303DLH_Wakeup(void);



#endif
